package com.qianbao.qianbaobusiness.ui.widget;

import android.animation.LayoutTransition;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;

import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.FeedBackTagBean;

import java.util.ArrayList;
import java.util.List;

public class RouteeGridLayout extends GridLayout {
    private View                dragedView;
    private OnItemClickListener mOnItemClickListener;

    private OnLongClickListener olcl = new OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            view.startDrag(null, new DragShadowBuilder(view), null, 0);
            view.setEnabled(false);
            dragedView = view;
            return false;
        }
    };

    public RouteeGridLayout(Context context) {
        this(context,null);
    }

    public RouteeGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setColumnCount(2);
        this.setLayoutTransition(new LayoutTransition());
    }

    public void setItems(List<FeedBackTagBean.FeedbackTagSceneCfgBean.TagListBean> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i).getTagWords();
            addItem(s);
        }
    }

    public void addItem(String s) {
        TextView tv = newTextViewItem();
        tv.setText(s);
        this.addView(tv);
    }

    private TextView newTextViewItem() {
        int margin = (int) getResources().getDimension(R.dimen.x5);
        LayoutParams params = new LayoutParams();
        params.width = (int) getResources().getDimension(R.dimen.x161);
        params.setMargins(margin, margin, margin, margin);
        TextView tv = new TextView(getContext());
        tv.setBackgroundResource(R.drawable.selector_feedback_tag);
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(Color.parseColor("#4A4A4A"));
        tv.setTextSize(12.f);
        tv.setSelected(false);
        tv.setPadding(0,(int)(getResources().getDimension(R.dimen.y15)),0,(int)(getResources().getDimension(R.dimen.y15)));
        tv.setLayoutParams(params);
        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick((TextView) view);
                }
            }
        });
        return tv;
    }

    public List<String> getDatas() {
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < this.getChildCount(); i++) {
            TextView tv = (TextView) this.getChildAt(i);
            datas.add(tv.getText().toString());
        }
        return datas;
    }

    public interface OnItemClickListener{
        void onItemClick(TextView view);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }
}
